package com.laomou.logdog;

import com.laomou.logdog.loginfo.LogInfo;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Callback;

public class ColorTableCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
    private String COLOR_VERBOSE = "#000000";
    private String COLOR_DEBUG = "#0000AA";
    private String COLOR_ERROR = "#FF0000";
    private String COLOR_FATAL = "#FF0000";
    private String COLOR_INFO = "#009A00";
    private String COLOR_WARN = "#FF9A00";


    private int m_fFontSize = 12;

    public void setFontSize(int nFontSize) {
        m_fFontSize = nFontSize;
    }

    @Override
    public TableCell<S, T> call(TableColumn<S, T> param) {
        return new TableCell<S, T>() {

            private String getString() {
                return getItem() == null ? "" : getItem().toString();
            }

            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, empty);
                setText(empty ? null : getString());
                TableRow currentRow = getTableRow();
                LogInfo logInfo = currentRow == null ? null : (LogInfo) currentRow.getItem();
                if (logInfo != null) {
                    switch (logInfo.getLogLv()) {
                        case "V":
                            setTextFill(Color.web(COLOR_VERBOSE));
                            break;
                        case "D":
                            setTextFill(Color.web(COLOR_DEBUG));
                            break;
                        case "W":
                            setTextFill(Color.web(COLOR_WARN));
                            break;
                        case "I":
                            setTextFill(Color.web(COLOR_INFO));
                            break;
                        case "E":
                            setTextFill(Color.web(COLOR_ERROR));
                            break;
                        case "F":
                            setTextFill(Color.web(COLOR_FATAL));
                            break;
                        default:
                            setTextFill(Color.web(COLOR_VERBOSE));
                            break;
                    }
                    setFont(Font.font(m_fFontSize));
                }
            }
        };
    }
}
